package com.shexianyu.service.impl;

import com.shexianyu.entity.Info;
import com.shexianyu.repository.InfoRepository;
import com.shexianyu.service.InfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shexianyu
 * @desc service实现类
 * @date 2022/1/19
 */
@Service
@Slf4j
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoRepository infoRepository;

    @Resource
    private RedisTemplate<String, String> redisTemplate;


    @Cacheable(value = "info")//缓存，未指定key
    public Info findById(long id) {
        log.info("从数据库中获取id========" + id);
        return infoRepository.findOne(id);
    }

    @CacheEvict(value = "info")
    public void deleteFromCache(long id) {
        log.info("删除缓存");
    }

    public void test() {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set("key_1", "random=" + Math.random());
        log.info(valueOperations.get("key_1"));

    }
}
